﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity.InterceptionExtension;

namespace Infrastructure.Web.Interceptor
{
    public  class PerformanceInspector: ICallHandler
    {
        public PerformanceInspector()
        { 

        }

        #region ICallHandler Members

        public IMethodReturn Invoke(IMethodInvocation input, GetNextHandlerDelegate getNext)
        {
            if (input.MethodBase is System.Reflection.MethodInfo)
            {   
                System.Diagnostics.Stopwatch wa = new System.Diagnostics.Stopwatch();
                wa.Start();
                var result = getNext()(input, getNext);
                wa.Stop();
                System.Web.HttpContext.Current.Trace.Warn(string.Format("Execution  {0}  time {1} ", input.MethodBase.Name, wa.ElapsedMilliseconds));
                return result;                
            }
            return getNext()(input, getNext);
        }

        public int Order
        {
            get;
            set;
        }

        #endregion
    }
}
